package db;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class Campaign {
	
	private PreparedStatement addCampaignStatement;
	private PreparedStatement updateCampaignStatement;
	private PreparedStatement deleteCampaignStatement;
	private PreparedStatement getCampaignsStatement;
	private PreparedStatement getCampaignDataStatement;

	// set connection to DBHelper
	private DBHelper helper = new DBHelper();

	// Declaring user data variables
	// Campaign name
	private String campaignName;

	// Campaign id
	private int campaignId;

	// Campaign description
	private String campaignDesc;

	// Campaign PointMultiplier
	private int PointMultiplier;

	// Campaign BeginDatetime
	private Date beginDatetime;

	// Campaign EndDatetime
	private Date endDatetime;

	// Campaign active status
	private int active;

	// Campaign CreatedBy
	private int CreatedBy;

	// Campaign ModifiedBy
	private int ModifiedBy;

	// Campaign birthdayPromo status
	private int birthdayPromo;

	/**
	 * Creates Campaign data object that contains the following
	 * 
	 * @param campaignName
	 * @param campaignId
	 * @param campaignDesc
	 * @param PointMultiplier
	 * @param BeginDatetime
	 * @param EndDatetime
	 * @param Active
	 * @param CreatedBy
	 * @param ModifiedBy
	 * @param birthdayPromo
	 */

	/**
	 * Generate Constructor
	 */

	public Campaign(String campaignName, int campaignId, String campaignDesc,
			int PointMultiplier, Date beginDatetime, Date endDatetime,
			int active, int CreatedBy, int ModifiedBy, int birthdayPromo) {
		this.campaignName = campaignName;
		this.campaignId = campaignId;
		this.campaignDesc = campaignDesc;
		this.PointMultiplier = PointMultiplier;
		this.beginDatetime = beginDatetime;
		this.endDatetime = endDatetime;
		this.active = active;
		this.CreatedBy = CreatedBy;
		this.ModifiedBy = ModifiedBy;
		this.birthdayPromo = birthdayPromo;

	}

	public Campaign() {
		
	}

	/**
	 * Adds or edit a Campaign
	 * 
	 * @param insertUpdate
	 *            1=update 0=insert
	 */
	public int addEditCampaign(int campaignId, int insertUpdate, String Name,
			String campaignDesc, int PointMultiplier, String BeginDatetime,
			String EndDatetime, int Active, int CreatedBy, int birthdayPromo) {
		int completed = 0;
		ResultSet rs = null;
		Connection conn = helper.connection();
		
		// insertUpdate 1=update 0=insert
		try {
			
			addCampaignStatement = conn.prepareStatement(DBHelper.addCampaignStatement);
			updateCampaignStatement = conn.prepareStatement(DBHelper.updateCampaignStatement);
			
			if (insertUpdate == 0) {
				addCampaignStatement.setString(1, Name);
				addCampaignStatement.setString(2, campaignDesc);
				addCampaignStatement.setInt(3, PointMultiplier);
				addCampaignStatement.setString(4,
						db.FormatAndValidation.convertDate(BeginDatetime));
				addCampaignStatement.setString(5,
						db.FormatAndValidation.convertDate(EndDatetime));
				addCampaignStatement.setInt(6, Active);
				addCampaignStatement.setInt(7, CreatedBy);
				addCampaignStatement.setInt(8, CreatedBy);// ModifiedBy
				addCampaignStatement.setInt(9, birthdayPromo);

				addCampaignStatement.executeUpdate();
			} else {
								
				updateCampaignStatement.setString(1, Name);
				updateCampaignStatement.setString(2, campaignDesc);
				updateCampaignStatement.setInt(3, PointMultiplier);
				updateCampaignStatement.setString(4,
						db.FormatAndValidation.convertDate(BeginDatetime));
				updateCampaignStatement.setString(5,
						db.FormatAndValidation.convertDate(EndDatetime));
				updateCampaignStatement.setInt(6, Active);
				updateCampaignStatement.setInt(7, CreatedBy);
				updateCampaignStatement.setInt(8, birthdayPromo);

				updateCampaignStatement.setInt(9, campaignId);

				updateCampaignStatement.executeUpdate();
			}

			System.out.println(Name);
			System.out.println(campaignDesc);
			System.out.println(PointMultiplier);
			System.out.println(BeginDatetime);
			System.out.println(EndDatetime);
			System.out.println(Active);
			System.out.println(CreatedBy);
			System.out.println(campaignId);

		}

		catch (Exception e) {

			System.out.println("Error in addEditCampaign: "
					+ e.getClass().getName() + ":" + e.getMessage());
			completed = 10;
		} finally {
			DBHelper.disconnect(rs, addCampaignStatement, conn);
			DBHelper.disconnect(rs, updateCampaignStatement, conn);
			
		}
		return completed;
	}

	/** Returns the delete's campaign info data */
	public int removeCampaign(int campaignId) {
		int validate = 0;
		ResultSet rs = null;
		Connection conn = helper.connection();
		
		try {
			
			deleteCampaignStatement = conn.prepareStatement(DBHelper.deleteCampaignStatement);

			deleteCampaignStatement.setInt(1, campaignId);

			deleteCampaignStatement.executeUpdate();

			System.out.println(validate);

		}

		catch (Exception e) {

			System.out.println("Error in removeCampaign: "
					+ e.getClass().getName() + ":" + e.getMessage());
			validate = 10;
		} finally {
			DBHelper.disconnect(rs, deleteCampaignStatement, conn);
		}
		return validate;
	}

	/** Returns the all Campaign info data */
	public ArrayList<Campaign> getAllCampaigninfo() {
		ArrayList<Campaign> campaignInfoData = new ArrayList<Campaign>();
		ResultSet rs = null;
		Connection conn = helper.connection();
		
		try {
			System.out.println("start query");
			
			getCampaignsStatement = conn.prepareStatement(DBHelper.getCampaignsStatement);
			
			rs = getCampaignsStatement.executeQuery();
			System.out.println("display rewards ");
			while (rs.next()) {
				System.out.println("display query"
						+ rs.getInt("PointMultiplier"));

				Campaign newCampaignData = new Campaign(rs.getString("Name"),
						rs.getInt("idCampaign"), rs.getString("Description"),
						rs.getInt("PointMultiplier"),
						rs.getDate("BeginDatetime"), rs.getDate("EndDatetime"),
						rs.getInt("Active"), rs.getInt("CreatedBy"),
						rs.getInt("ModifiedBy"), rs.getInt("birthdayPromo"));
				campaignInfoData.add(newCampaignData);
			}
		
		}

		catch (Exception e) {
			System.out.println("Error in getAllCampaigninfo: "
					+ e.getClass().getName() + ":" + e.getMessage());
		} finally {
			DBHelper.disconnect(rs, getCampaignsStatement, conn);
		}
		System.out.println("end query");
		return campaignInfoData;
	}

	/** Returns the individual Campaign info data */
	public ArrayList<Campaign> getCampaignInfo(int campaignId) {
		ArrayList<Campaign> campaignInfoData = new ArrayList<Campaign>();
		ResultSet rs = null;
		Connection conn = helper.connection();
		
		try {
			System.out.println("start query");
			
			getCampaignDataStatement = conn.prepareStatement(DBHelper.getCampaignDataStatement);
			
			getCampaignDataStatement.setInt(1, campaignId);

			rs = getCampaignDataStatement.executeQuery();
			System.out.println("display rewards ");
			while (rs.next()) {
				System.out.println("display query"
						+ rs.getInt("PointMultiplier"));

				Campaign newCampaignData = new Campaign(rs.getString("Name"),
						rs.getInt("idCampaign"), rs.getString("Description"),
						rs.getInt("PointMultiplier"),
						rs.getDate("BeginDatetime"), rs.getDate("EndDatetime"),
						rs.getInt("Active"), rs.getInt("CreatedBy"),
						rs.getInt("ModifiedBy"), rs.getInt("birthdayPromo"));
				campaignInfoData.add(newCampaignData);
			}
		}

		catch (Exception e) {
			System.out.println("Error in getCampaignInfo: "
					+ e.getClass().getName() + ":" + e.getMessage());
		} finally {
			DBHelper.disconnect(rs, getCampaignDataStatement, conn);
		}
		System.out.println("end query");
		return campaignInfoData;
	}

	public String getCampaignName() {
		return campaignName;
	}

	public void setCampaignName(String campaignName) {
		this.campaignName = campaignName;
	}

	public int getCampaignId() {
		return campaignId;
	}

	public void setCampaignId(int campaignId) {
		this.campaignId = campaignId;
	}

	public String getCampaignDesc() {
		return campaignDesc;
	}

	public void setCampaignDesc(String campaignDesc) {
		this.campaignDesc = campaignDesc;
	}

	public int getPointMultiplier() {
		return PointMultiplier;
	}

	public void setPointMultiplier(int pointMultiplier) {
		PointMultiplier = pointMultiplier;
	}

	public Date getBeginDatetime() {
		return beginDatetime;
	}

	public void setBeginDatetime(Date beginDatetime) {
		this.beginDatetime = beginDatetime;
	}

	public Date getEndDatetime() {
		return endDatetime;
	}

	public void setEndDatetime(Date endDatetime) {
		this.endDatetime = endDatetime;
	}

	public int getActive() {
		return active;
	}

	public void setActive(int active) {
		this.active = active;
	}

	public int getCreatedBy() {
		return CreatedBy;
	}

	public void setCreatedBy(int createdBy) {
		CreatedBy = createdBy;
	}

	public int getModifiedBy() {
		return ModifiedBy;
	}

	public void setModifiedBy(int modifiedBy) {
		ModifiedBy = modifiedBy;
	}

	public int getBirthdayPromo() {
		return birthdayPromo;
	}

	public void setBirthdayPromo(int birthdayPromo) {
		this.birthdayPromo = birthdayPromo;
	}

}
